package com.ruoyi.kiku.service.impl;

import java.util.Collections;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.OssUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.kiku.mapper.JsslArtistsWorksMapper;
import com.ruoyi.kiku.domain.JsslArtistsWorks;
import com.ruoyi.kiku.service.IJsslArtistsWorksService;

/**
 * 橘事顺利艺术家代表作Service业务层处理
 *
 * @author TT
 * @date 2025-09-03
 */
@Service
public class JsslArtistsWorksServiceImpl implements IJsslArtistsWorksService
{
    @Autowired
    private JsslArtistsWorksMapper jsslArtistsWorksMapper;

    /**
     * 查询橘事顺利艺术家代表作
     *
     * @param id 橘事顺利艺术家代表作主键
     * @return 橘事顺利艺术家代表作
     */
    @Override
    public JsslArtistsWorks selectJsslArtistsWorksById(Long id)
    {
        return jsslArtistsWorksMapper.selectJsslArtistsWorksById(id);
    }

    /**
     * 查询橘事顺利艺术家代表作列表
     *
     * @param jsslArtistsWorks 橘事顺利艺术家代表作
     * @return 橘事顺利艺术家代表作
     */
    @Override
    public List<JsslArtistsWorks> selectJsslArtistsWorksList(JsslArtistsWorks jsslArtistsWorks)
    {
        return jsslArtistsWorksMapper.selectJsslArtistsWorksList(jsslArtistsWorks);
    }

    /**
     * 新增橘事顺利艺术家代表作
     *
     * @param jsslArtistsWorks 橘事顺利艺术家代表作
     * @return 结果
     */
    @Override
    public int insertJsslArtistsWorks(JsslArtistsWorks jsslArtistsWorks)
    {
        jsslArtistsWorks.setCreateTime(DateUtils.getNowDate());
        return jsslArtistsWorksMapper.insertJsslArtistsWorks(jsslArtistsWorks);
    }

    /**
     * 修改橘事顺利艺术家代表作
     *
     * @param jsslArtistsWorks 橘事顺利艺术家代表作
     * @return 结果
     */
    @Override
    public int updateJsslArtistsWorks(JsslArtistsWorks jsslArtistsWorks)
    {
        jsslArtistsWorks.setUpdateTime(DateUtils.getNowDate());
        return jsslArtistsWorksMapper.updateJsslArtistsWorks(jsslArtistsWorks);
    }

    /**
     * 批量删除橘事顺利艺术家代表作
     *
     * @param ids 需要删除的橘事顺利艺术家代表作主键
     * @return 结果
     */
    @Override
    public int deleteJsslArtistsWorksByIds(Long[] ids)
    {
        for (int i = 0; i < ids.length; i++) {
            delectOssImageAndVideo(ids[i]);
        }
        return jsslArtistsWorksMapper.deleteJsslArtistsWorksByIds(ids);
    }

    /**
     * 删除橘事顺利艺术家代表作信息
     *
     * @param id 橘事顺利艺术家代表作主键
     * @return 结果
     */
    @Override
    public int deleteJsslArtistsWorksById(Long id)
    {
        delectOssImageAndVideo(id);
        return jsslArtistsWorksMapper.deleteJsslArtistsWorksById(id);
    }

    /**
     * 根据艺术家id获取该艺术家的代表作
     * @param artistsId 艺术家ID
     * @return List<JsslArtistsWorks>
     */
    @Override
    public List<JsslArtistsWorks> getArtistsMasterpiece(Long artistsId) {
        return jsslArtistsWorksMapper.getArtistsMasterpiece(artistsId);
    }
    /**
     * 删除OSS图片和视频
     * @param id 作品id
     */
    public void delectOssImageAndVideo(Long id){
        JsslArtistsWorks jsslArtistsWorks = jsslArtistsWorksMapper.selectJsslArtistsWorksById(id);
        String imageUrl = jsslArtistsWorks.getPoster();
        String videoUrl = jsslArtistsWorks.getTrailer();
        if (!StringUtils.isEmpty(imageUrl)){
            OssUtils.delete(imageUrl);
        }
        if (!StringUtils.isEmpty(videoUrl)){
            OssUtils.delete(videoUrl);
        }
    }

    /**
     * 查询小鞠的作品
     * @param jsslArtistsWorks 橘事顺利艺术家代表作
     * @return
     */
    @Override
    public List<JsslArtistsWorks> selectJsslArtistsWorksList2(JsslArtistsWorks jsslArtistsWorks) {
        List<JsslArtistsWorks> jsslArtistsWorksList = jsslArtistsWorksMapper.selectJsslArtistsWorksList2(jsslArtistsWorks);
        if (jsslArtistsWorksList != null){
            for (int i = 0; i < jsslArtistsWorksList.size() ; i++) {
                JsslArtistsWorks jsslArtistsWorks1 = jsslArtistsWorksList.get(i);
                jsslArtistsWorks1.setActorsArray(StringUtils.splitToArray(jsslArtistsWorks1.getActors()));
                jsslArtistsWorks1.setWorkTagsArray(StringUtils.splitToArray(jsslArtistsWorks1.getWorkTags()));
                jsslArtistsWorksList.get(i).setWorkTagsArray(jsslArtistsWorks1.getWorkTagsArray());
                jsslArtistsWorksList.get(i).setActorsArray(jsslArtistsWorks1.getActorsArray());

            }
        }
        return jsslArtistsWorksList;
    }
}
